package com.lc.ck.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lc.ck.common.QueryPageParam;
import com.lc.ck.common.Result;
import com.lc.ck.entity.Goods;
import com.lc.ck.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/goods")
public class GoodsController {

        @Autowired
        private GoodsService goodsService;

        @PostMapping("/save")
        public Result save(@RequestBody Goods goods){

            return goodsService.save(goods)?Result.suc():Result.fail();
        }
        //修改
        @PostMapping("/mod")
        public boolean mod(@RequestBody Goods goods){
            return goodsService.updateById(goods);
        }
        //更新
        @PostMapping("/update")
        public Result update(@RequestBody Goods goods){

            return goodsService.updateById(goods)?Result.suc():Result.fail();
        }
        //新增或修改
        @PostMapping("/saveOrMod")
        public  boolean saveOrMod(@RequestBody Goods goods){
            return goodsService.saveOrUpdate(goods);
        }
        //删除
        @GetMapping("/delete")
        public Result delete(@RequestParam String id){
            return goodsService.removeById(id)?Result.suc():Result.fail();
        }

        @PostMapping("/listPlist")//返回数据封装
        public Result listPlist(@RequestBody QueryPageParam query){
            HashMap param = query.getParam();
            String name = (String)param.get("name");
            String goodstype = (String)param.get("goodstype");
            String storage = (String)param.get("storage");

            Page<Goods> page = new Page();
            page.setCurrent(query.getPageNum());
            page.setSize(query.getPageSize());

            LambdaQueryWrapper<Goods> lambdaQueryWrapper = new LambdaQueryWrapper();
            if(StringUtils.isNotBlank(name) && !"null".equals(name)){//非空判断和非 null 判断
                lambdaQueryWrapper.like(Goods::getName,name);
            }
            if(StringUtils.isNotBlank(storage) && !"null".equals(storage)){//非空判断和非 null 判断
                lambdaQueryWrapper.eq(Goods::getStorage,storage);
            }
            if(StringUtils.isNotBlank(goodstype) && !"null".equals(goodstype)){//非空判断和非 null 判断
                lambdaQueryWrapper.eq(Goods::getGoodstype,goodstype);
            }

            IPage result = goodsService.pageCC(page,lambdaQueryWrapper);

            return Result.suc(result.getRecords(),result.getTotal());//成功时将数据和总数返回
        }



}
